package org.demo.spring.data.transaction;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

@Service
public class UserService{


    @Autowired
    private DataSource dataSource;

    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,rollbackForClassName = "java.lang.Exception")
    public void say() throws SQLException {
        System.out.println("你好，事务");
        Connection connection = dataSource.getConnection();
        String sql = "UPDATE user SET userName=? WHERE id=?";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setString(1,"c");
        statement.setInt(2,2);
        statement.executeUpdate();
        String name = Exception.class.getName();
        System.out.println(name);
        int a = 1/0;
    }

}
